package com.aidong.service;

/**
 * Created by john on 16-11-2.
 */
import com.aidong.cache.ItemCache;
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.wills.PssPurcReceiveDao;
import com.aidong.dao.wills.PssPurcReceiveListDao;
import com.aidong.dao.wills.PssPurcReturnDao;
import com.aidong.dao.wills.PssPurcReturnListDao;
import com.aidong.dao.wills.PssTranSendDao;
import com.aidong.dao.wills.PssTranSendListDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssPurcReceive;
import com.aidong.model.wills.PssPurcReceiveImpl;
import com.aidong.model.wills.PssPurcReturn;
import com.aidong.model.wills.PssPurcReturnImpl;
import com.aidong.model.wills.PssTranSend;
import com.aidong.model.wills.PssTranSendImpl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class PssPrintDataService {
    @Resource
    protected ItemCache cache;
    @Resource
    PssPurcReceiveDao pssPurcReceiveDao;
    @Resource
    PssPurcReceiveListDao pssPurcReceiveListDao;
    @Resource
    PssPurcReturnDao pssPurcReturnDao;
    @Resource
    PssPurcReturnListDao pssPurcReturnListDao;
    @Resource
    PssTranSendDao pssTranSendDao;
    @Resource
    PssTranSendListDao pssTranSendListDao;

    public PssPrintDataService() {
    }

    public String getPssReceiveData(Integer receiveID, StringBuilder msg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ConditionMap cd = new ConditionMap();
        cd.eq("ReceiveID", receiveID);
        PssPurcReceive psspr = (PssPurcReceive)this.pssPurcReceiveDao.getById(receiveID);
        psspr.setPrintLabel("Y");
        this.pssPurcReceiveDao.update(psspr);
        List lst = this.pssPurcReceiveListDao.queryPurcReceiveRptList(cd);
        String shopName = this.nvl(this.cache.getValue("shopName", psspr.getShopCode()));
        StringBuilder sb = new StringBuilder();
        sb.append("{receiveId:\"").append(String.valueOf(receiveID)).append("\"").append(",shopName:\"").append(shopName).append("\"").append(",printDate:\"").append(sdf.format(new Date())).append("\"").append(",lstLength:\"").append(lst.size()).append("\"");
        int i = 0;

        for(Iterator i$ = lst.iterator(); i$.hasNext(); ++i) {
            PssPurcReceiveImpl pssprl = (PssPurcReceiveImpl)i$.next();
            sb.append(",productCode").append(String.valueOf(i)).append(":\"").append(pssprl.getProductCode()).append("\"");
            sb.append(",productName").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("ProductcodeList", pssprl.getProductCode()))).append("\"");
            sb.append(",unit").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("productUnit", pssprl.getUnit()))).append("\"");
            sb.append(",qty").append(String.valueOf(i)).append(":\"").append(pssprl.getQuantity()).append("\"");
            sb.append(",providerName").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("providerList", pssprl.getProviderCode()))).append("\"");
        }

        sb.append("}");
        return sb.toString();
    }

    public String getPssReturnData(Integer returnID, StringBuilder msg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ConditionMap cd = new ConditionMap();
        cd.eq("ReturnID", returnID);
        PssPurcReturn psspr = (PssPurcReturn)this.pssPurcReturnDao.getById(returnID);
        psspr.setPrintLabel("Y");
        this.pssPurcReturnDao.update(psspr);
        List lst = this.pssPurcReturnListDao.queryPurcReturnRptList(cd);
        String shopName = this.nvl(this.cache.getValue("shopName", psspr.getShopCode()));
        StringBuilder sb = new StringBuilder();
        sb.append("{receiveId:\"").append(String.valueOf(returnID)).append("\"").append(",shopName:\"").append(shopName).append("\"").append(",printDate:\"").append(sdf.format(new Date())).append("\"").append(",lstLength:\"").append(lst.size()).append("\"");
        int i = 0;

        for(Iterator i$ = lst.iterator(); i$.hasNext(); ++i) {
            PssPurcReturnImpl pssprl = (PssPurcReturnImpl)i$.next();
            sb.append(",productCode").append(String.valueOf(i)).append(":\"").append(pssprl.getProductCode()).append("\"");
            sb.append(",productName").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("ProductcodeList", pssprl.getProductCode()))).append("\"");
            sb.append(",unit").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("productUnit", pssprl.getUnit()))).append("\"");
            sb.append(",qty").append(String.valueOf(i)).append(":\"").append(pssprl.getQuantity()).append("\"");
            sb.append(",providerName").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("providerList", pssprl.getProviderCode()))).append("\"");
        }

        sb.append("}");
        return sb.toString();
    }

    public String getPssSaleData(String shopCode, Double amount, Double payAmount, String payType, String salesCode, Products products, StringBuilder msg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String shopName = this.nvl(this.cache.getValue("shopName", shopCode));
        String payTypeName = this.nvl(this.cache.getValue("payType", payType));
        String salesName = salesCode + "-" + this.nvl(this.cache.getValue("salesName", salesCode));
        StringBuilder sb = new StringBuilder();
        sb.append("{shopName:\"").append(shopName).append("\"").append(",printDate:\"").append(sdf.format(new Date())).append("\"").append(",amount:\"").append(String.valueOf(amount)).append("\"").append(",salesName:\"").append(salesName).append("\"").append(",payTypeName:\"").append(payTypeName).append("\"").append(",payAmount:\"").append(String.valueOf(payAmount)).append("\"").append(",changeAmount:\"").append(String.valueOf(payAmount.doubleValue() - amount.doubleValue())).append("\"").append(",lstLength:\"").append(products.getLstProduct().size() - 1).append("\"");
        int i = 0;
        Iterator i$ = products.getLstProduct().iterator();

        while(i$.hasNext()) {
            PssPopProduct pc = (PssPopProduct)i$.next();
            if(!"".equals(this.nvl(pc.getProductCode()))) {
                sb.append(",productName").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("ProductcodeList", pc.getProductCode()))).append("\"");
                sb.append(",qty").append(String.valueOf(i)).append(":\"").append(pc.getQty()).append("\"");
                ++i;
            }
        }

        sb.append("}");
        return sb.toString();
    }

    public String getPssTranData(Integer sendID, StringBuilder msg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        PssTranSend psspr = (PssTranSend)this.pssTranSendDao.getById(sendID);
        psspr.setPrintLabel("Y");
        this.pssTranSendDao.update(psspr);
        ConditionMap cd = new ConditionMap();
        cd.eq("SendID", sendID);
        List lst = this.pssTranSendListDao.queryTranSendList(cd);
        String shopName = this.nvl(this.cache.getValue("shopName", psspr.getReceiveShop()));
        StringBuilder sb = new StringBuilder();
        sb.append("{sendID:\"").append(String.valueOf(sendID)).append("\"").append(",shopName:\"").append(shopName).append("\"").append(",printDate:\"").append(sdf.format(new Date())).append("\"").append(",lstLength:\"").append(lst.size()).append("\"");
        int i = 0;

        for(Iterator i$ = lst.iterator(); i$.hasNext(); ++i) {
            PssTranSendImpl pssprl = (PssTranSendImpl)i$.next();
            sb.append(",productCode").append(String.valueOf(i)).append(":\"").append(pssprl.getProductCode()).append("\"");
            sb.append(",productName").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("ProductcodeList", pssprl.getProductCode()))).append("\"");
            sb.append(",unit").append(String.valueOf(i)).append(":\"").append(this.nvl(this.cache.getValue("productUnit", pssprl.getUnit()))).append("\"");
            sb.append(",qty").append(String.valueOf(i)).append(":\"").append(pssprl.getQuantity()).append("\"");
        }

        sb.append("}");
        return sb.toString();
    }

    protected String nvl(String s) {
        return s == null?"":s;
    }

    private String retMsg(StringBuilder sb, String msg) {
        if(sb != null) {
            sb.append(msg);
        }

        return null;
    }
}

